package com.ruangong.GitClient.Test.GiteeAPITest;


import android.util.Log;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import com.ruangong.GitClient.GiteeAPI.GiteeFile;
import com.ruangong.GitClient.GiteeAPI.GiteeRepository;
import com.ruangong.GitClient.GiteeAPI.GiteeUser;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Test2 {

    //    private static OkHttpClient client = new OkHttpClient();
    private static final String BASE_URL = "https://gitee.com/api/v5/repos/";
    private static final String ACCESS_TOKEN = "7735112db6b139495f3883030bc1ac98"; // 请替换为你的访问令牌

    public static void main(String[] args) {
        GiteeUser giteeUser = new GiteeUser(ACCESS_TOKEN,"9649b65faee309bbae1eec463a973a4edfcb25e166ae6a57387cc55130a5b777");
        giteeUser.showInfo();
        String repo = "ruan-gong-project"; // 仓库名称
//        exploreRepository("SilverXQ", repo, ""); // 从根目录开始
//        OkHttpClient client = new OkHttpClient();
        String owner = "SilverXQ";
        String url = BASE_URL + owner + "/" + repo + "?access_token=" + ACCESS_TOKEN;
//        Request request = new Request.Builder()
//                .url(url)
//                .build();

        try (Response response = GiteeUser.client.newCall(new Request.Builder().url(url).build()).execute()) {
            if (response.isSuccessful()) {
                assert response.body() != null;
                String responseBody = response.body().string();
                System.out.println(responseBody);
                GiteeRepository giteeRepository = GiteeRepository.fromJson(responseBody);
//                System.out.println(giteeRepository);
                List<GiteeFile> giteeFiles = giteeRepository.getFiles();
                for (GiteeFile giteeFile : giteeFiles) {
//                    System.out.println("Type: " + giteeFile.getType() + ", Name: " + giteeFile.getName() + ", Path: " + giteeFile.getPath());

                    if ("dir".equals(giteeFile.getType())) {
                        giteeFile.showDirContent();
//                        giteeFile.showDirAllFiles();
                    } else if ("file".equals(giteeFile.getType())) {
                        giteeFile.showFileInfo();
                    }
                }

            } else {
                System.err.println("Failed to fetch repository contents: " + response.message());
            }
        } catch (IOException e) {
            Log.e("Test2", "Error making HTTP request", e);
        }



        String url2 = BASE_URL + owner + "/" + repo + "/contents/.gitignore?access_token=" + ACCESS_TOKEN;
//        Request request2 = new Request.Builder()
//                .url(url2)
//                .build();
        try (Response response = GiteeUser.client.newCall(new Request.Builder().url(url2).build()).execute()) {
            if (response.isSuccessful()) {
                assert response.body() != null;
                String responseBody = response.body().string();
                JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject(); // 解析 JSON 字符串为 JsonObject

                // 安全地从 JsonObject 中获取 content 字符串
                String encodedContent = jsonObject.has("content") ? jsonObject.get("content").getAsString() : null;

                // 检查 encodedContent 是否为空
                if (encodedContent != null && !encodedContent.isEmpty()) {
                    // 解码 Base64 编码的 content
//                    byte[] decodedBytes = Base64.getDecoder().decode(encodedContent);
//                    String decodedContent = new String(decodedBytes, StandardCharsets.UTF_8);
                    String decodedContent = new String(java.util.Base64.getDecoder().decode(encodedContent), StandardCharsets.UTF_8);
//                    String decodedContent = new String(android.util.Base64.decode(encodedContent, Base64.DEFAULT), StandardCharsets.UTF_8);
//                    return decodedContent;
                    System.out.println(decodedContent);

                } else {
                    System.out.println("No content to decode");
//                    return null;
                }
            } else {
                throw new RuntimeException("Failed to fetch data: " + response.message());
            }
        } catch (IOException e) {
            throw new RuntimeException("Error making HTTP request", e);
        }


    }


}

